package org.swinglife.log.sys;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/***
 * Created by Swinglife on 2018/5/25.
 */
public  class SystemLog  implements SLSystemLog {

    private String savePath = "/opt/log";

    private String logFileName = "";

    public SystemLog(String savePath){
        this.savePath = savePath;
    }


    public SystemLog(String savePath,String logFileName){
        this.savePath = savePath;
        this.logFileName = logFileName;
    }

    /***
     * 写日志
     * @param message
     */
    @Override
    public void message(String message){
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
        String fileName = null;
        if(logFileName!="") {
            fileName = logFileName+"_"+dateFm.format(new Date()) + ".log";
        }else{
            fileName = dateFm.format(new Date()) + ".log";
        }
        File file = new File(savePath);
        if(!file.exists()){
            file.mkdirs();
        }
        File f = new File(savePath+"/"+fileName);
        writeFile(message,f);

    }

    @Override
    public void debug(String debug) {

    }

    /****
     * 输出到文件
     * @param message
     * @param f
     */
    private void writeFile(String message,File f){
        SimpleDateFormat dateFm = null;
        if (f.exists()) {
        } else {
            System.out.print("文件不存在");
            try {
                f.createNewFile();// 不存在则创建
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            BufferedWriter output = new BufferedWriter(new FileWriter(f,true));//true,则追加写入text文本
            dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //格式化当前系统日期
            output.write(dateFm.format(new Date())+" MESSAGE==>>:"+message);
            output.write("\r\n");//换行
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * 创建错误日志的文件
     * @return
     */
    private File createErrorLogFile(){
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
        String fileName = null;
        if(logFileName!="") {
            fileName = logFileName+"_"+dateFm.format(new Date()) + "_error.log";
        }else{
            fileName = dateFm.format(new Date()) + "_error.log";
        }
        File file = new File(savePath);
        if(!file.exists()){
            file.mkdirs();
        }
        File f = new File(savePath+"/"+fileName);
        return f;
    }


    /****
     * 写入错误日志 并且将异常写入
     * @param message
     * @param e
     */
    @Override
    public void error(String message,Exception e){
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        e.printStackTrace(printWriter);
        //输出的错误信息
        String exceptionMsg = stringWriter.toString();
        StringBuffer sb = new StringBuffer(message+"\n");
        sb.append("!!!!Exception:"+exceptionMsg+"\n\n");
        writeFile(sb.toString(),createErrorLogFile());
    }


    /***
     * 写日志 错误日志
     * @param message
     */
    @Override
    public void error(String message){
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
        String fileName = null;
        if(logFileName!="") {
            fileName = logFileName+"_"+dateFm.format(new Date()) + "_error.log";
        }else{
            fileName = dateFm.format(new Date()) + "_error.log";
        }
        File file = new File(savePath);
        if(!file.exists()){
            file.mkdirs();
        }
        File f = new File(savePath+"/"+fileName);
        writeFile(message,f);

    }



}
